package cn.dlc.com.user.service.impl;

import cn.dlc.com.user.model.User;
import cn.dlc.com.user.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

/**
 * @author 邓立川
 * @date 2021/11/21 10:15
 * @desc 当前类的描述信息
 */
@Service
public class UserServiceImpl implements UserService {

    @Override
    public User authentication(User user) {
        // 模拟数据库查询

        if(StringUtils.isEmpty(user.getUsername())) {
            throw new RuntimeException("username is empty！");
        } else {
            User userFromDB = this.getUserByUsername(user.getUsername());
            if(userFromDB == null || !user.getPassword().equals(userFromDB.getPassword())) {
                throw new RuntimeException("username or password error");
            }
            return userFromDB;
        }

    };

    private User getUserByUsername(String username){

        return userMap.get(username);
    }

    private Map<String, User> userMap= new HashMap<>();
    {
        Set<String> authenticat1 = new TreeSet<>();
        authenticat1.add("p1");
        Set<String> authenticat2 = new TreeSet<>();
        authenticat2.add("p2");
        userMap.put("jack", new User("jack","123",authenticat1));
        userMap.put("rose", new User("rose","123", authenticat2));
    }
}
